package com.shouer.doc.ui.ordermanager.presenter;


import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.ordermanager.response.QueryFinishedHealthRecordResponse;
import com.shouer.doc.ui.ordermanager.response.QueryHealthRecordResponse;
import com.shouer.doc.ui.ordermanager.view.IOrderManagerView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY on 2017/8/9.
 */

public class HealthRecordPresenterCompl implements IHealthRecordPresenter{

    private IOrderManagerView mView;

    private int currentPageNo1 = 1;//进行中订单查询分页数
    private int currentPageNo2 = 1;//已完成订单查询分页数
    private int maxPageNo = 20;
    private int pageNum1= 20;
    private int pageNum2 = 50;

    private boolean isHealthRefreshing1=false;
    private boolean isHealthLoadingMore1=false;

    private boolean isHealthRefreshing2=false;
    private boolean isHealthLoadingMore2=false;

    public HealthRecordPresenterCompl(IOrderManagerView mView)
    {
        this.mView=mView;
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void queryHealthRecord(int pageNo, int pageNum) {

        Map<String, String> param = new HashMap<>();
        param.put("pageNo", String.valueOf(pageNo));
        param.put("pageNum", String.valueOf(pageNum));
        param.put("docId", mView.getUser().getUserId());
        param.put("state","3");

        if (pageNo == 1) {
            isHealthRefreshing1 = true;
        } else {
            isHealthLoadingMore1 = true;
        }

        final GsonRequest<QueryHealthRecordResponse> request = new GsonRequest("queryHealthRecord", QueryHealthRecordResponse.class, new Response.Listener<QueryHealthRecordResponse>() {
            @Override
            public void onResponse(QueryHealthRecordResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isHealthRefreshing1 = false;
                isHealthLoadingMore1 = false;
                mView.loadHealthRecordFailed();

            }
        });

        RequestUtil.sendRequest(mView.getContext(), param, request);

    }

    @Override
    public void queryFinishedHealthRecord(int pageNo, int pageNum) {

        Map<String, String> param = new HashMap<>();
        param.put("pageNo", String.valueOf(pageNo));
        param.put("pageNum", String.valueOf(pageNum));
        param.put("docId", mView.getUser().getUserId());
        param.put("state","4,5");

        if (pageNo == 1) {
            isHealthRefreshing2 = true;
        } else {
            isHealthLoadingMore2 = true;
        }

        final GsonRequest<QueryFinishedHealthRecordResponse> request = new GsonRequest("queryHealthRecord", QueryFinishedHealthRecordResponse.class, new Response.Listener<QueryFinishedHealthRecordResponse>() {
            @Override
            public void onResponse(QueryFinishedHealthRecordResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isHealthRefreshing2 = false;
                isHealthLoadingMore2 = false;
                mView.loadFinishedHealthRecordFailed();

            }
        });

        RequestUtil.sendRequest(mView.getContext(), param, request);

    }

    @Override
    public boolean refreshHealthRecord() {
        if (isHealthRefreshing1 || isHealthLoadingMore1 || !mView.getApp().isUserLogin()) {
            return false;
        }
        queryHealthRecord(1, pageNum1);
        return true;
    }

    @Override
    public boolean loadMoreHealthRecord() {
        if (isHealthRefreshing1 || isHealthLoadingMore1 || (currentPageNo1 + 1) > maxPageNo || !mView.getApp().isUserLogin()) {
            return false;
        }
        queryHealthRecord(currentPageNo1+1, pageNum1);
        return true;
    }

    @Override
    public boolean refreshFinishedHealthRecord() {
        if (isHealthRefreshing2 || isHealthLoadingMore2 || !mView.getApp().isUserLogin()) {
            return false;
        }
        queryFinishedHealthRecord(1, pageNum2);
        return true;
    }

    @Override
    public boolean loadMoreFinishedHealthRecord() {
        if (isHealthRefreshing2 || isHealthLoadingMore2 || (currentPageNo2 + 1) > maxPageNo || !mView.getApp().isUserLogin()) {
            return false;
        }
        queryFinishedHealthRecord(currentPageNo2+1, pageNum2);
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryHealthRecordResponse response) {
        isHealthRefreshing1 = false;
        isHealthLoadingMore1 = false;
        if (response.getErrorCode() >= 0) {
            if (response.getPageNo() == 1) {
                currentPageNo1 = 1;
                mView.refreshHealthRecordList(response.getRecordList());
            } else {
                if (null != response.getRecordList() && response.getRecordList().size() > 0) {
                    currentPageNo1++;
                }
                mView.attachHealthRecordList(response.getRecordList());
            }
        } else {
            mView.loadHealthRecordFailed();
            Toast.makeText(mView.getContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryFinishedHealthRecordResponse response) {
        isHealthRefreshing2 = false;
        isHealthLoadingMore2 = false;
        if (response.getErrorCode() >= 0) {
            if (response.getPageNo() == 1) {
                currentPageNo2 = 1;
                mView.refreshFinishedHealthRecordList(response.getRecordList());
            } else {
                if (null != response.getRecordList() && response.getRecordList().size() > 0) {
                    currentPageNo2++;
                }
                mView.attachFinishedHealthRecordList(response.getRecordList());
            }
        } else {
            mView.loadFinishedHealthRecordFailed();
            Toast.makeText(mView.getContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
        }
    }
}
